//
//  MyTabBarController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/13.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "MyTabBarController.h"
#import "HomeViewController.h"
#import "ShoppingViewController.h"
#import "OrderViewController.h"
#import "MeViewController.h"

@interface MyTabBarController ()

@property (strong, nonatomic) HomeViewController *homeViewController;
@property (strong, nonatomic) ShoppingViewController *shoppingViewController;
@property (strong, nonatomic) OrderViewController *orderViewController;
@property (strong, nonatomic) MeViewController *meViewController;

@end

@implementation MyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tabBar setTintColor:I_COLOR_RED];
    [self.tabBar setBackgroundColor:I_COLOR_WHITE];
    [self.tabBar setBarTintColor:I_COLOR_WHITE];
    
    self.homeViewController = [[HomeViewController alloc] init];
    UINavigationController *homeNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
    UIImage *homeImage = [UIImage imageNamed:@"ic_tabbar_home_off"];
    UIImage *homeSelectedImage = [UIImage imageNamed:@"ic_tabbar_home_on"];
    homeNavigationController.title = @"首页";
    homeNavigationController.tabBarItem.image = homeImage;
    homeNavigationController.tabBarItem.selectedImage = homeSelectedImage;
    
    self.shoppingViewController = [[ShoppingViewController alloc] init];
    UINavigationController *shoppingNavigationController = [[UINavigationController alloc] initWithRootViewController:self.shoppingViewController];
    UIImage *shoppingImage = [UIImage imageNamed:@"ic_tabbar_shopping_off"];
    UIImage *shoppingSelectedImage = [UIImage imageNamed:@"ic_tabbar_shopping_on"];
    shoppingNavigationController.title = @"商城";
    shoppingNavigationController.tabBarItem.image = shoppingImage;
    shoppingNavigationController.tabBarItem.selectedImage = shoppingSelectedImage;
    
    self.orderViewController = [[OrderViewController alloc] init];
    [self.orderViewController registerNotifications];
    UINavigationController *orderNavigationController = [[UINavigationController alloc] initWithRootViewController:self.orderViewController];
    UIImage *orderImage = [UIImage imageNamed:@"ic_tabbar_order_off"];
    UIImage *orderSelectedImage = [UIImage imageNamed:@"ic_tabbar_order_on"];
    orderNavigationController.title = @"我的订单";
    orderNavigationController.tabBarItem.image = orderImage;
    orderNavigationController.tabBarItem.selectedImage = orderSelectedImage;
    
    self.meViewController = [[MeViewController alloc] init];
    [self.meViewController registerNotifications];
    UINavigationController *meNavigationController = [[UINavigationController alloc] initWithRootViewController:self.meViewController];
    UIImage *meImage = [UIImage imageNamed:@"ic_tabbar_me_off"];
    UIImage *meSelectedImage = [UIImage imageNamed:@"ic_tabbar_me_on"];
    meNavigationController.title = @"我的";
    meNavigationController.tabBarItem.image = meImage;
    meNavigationController.tabBarItem.selectedImage = meSelectedImage;
    
    self.viewControllers = [NSArray arrayWithObjects:homeNavigationController, shoppingNavigationController, orderNavigationController, meNavigationController, nil];
    
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       I_COLOR_TAB_TEXT, NSForegroundColorAttributeName,
                                                       [UIFont systemFontOfSize:11.0f], NSFontAttributeName,nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       I_COLOR_RED, NSForegroundColorAttributeName,[UIFont systemFontOfSize:11.0f], NSFontAttributeName,
                                                       nil] forState:UIControlStateSelected];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOrderNotifyAction:) name:kNotificationRefreshOrder object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSString *title = item.title;
    if([title isEqualToString:@"我的订单"]) {
        [self.orderViewController viewDidReload];
    }
    
}


#pragma mrak - Action
- (void)refreshOrderNotifyAction:(NSNotification *)notification {
    if(self.selectedIndex != 2) {
        [self setSelectedIndex:2];
        [self.orderViewController viewDidReload];
    }
}


@end
